home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / MacPNG Library 1.02 / pngMacSrc 1.02 / png.1 / ptot / crc32.c next >
Encoding:
C/C++ Source or Header  |  1995-08-17  |  1.1 KB  |  64 lines  |  [TEXT/CWIE]

  1. /*
  2.  * crc32.c
  3.  *
  4.  * Function to calculate 32-bit CRC values for PNG chunks.
  5.  *
  6.  **********
  7.  *
  8.  * HISTORY
  9.  *
  10.  * 95-03-10 Created by Lee Daniel Crocker <lee@piclab.com>
  11.  *          <URL:http://www.piclab.com/piclab/index.html>
  12.  */
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17. #include "ptot.h"
  18.  
  19. static U32 crc_table[256] = { 0xFFFFFFFFL };
  20. static void build_crc_table(U32 *);
  21.  
  22. U32
  23. update_crc(
  24.     U32 input_crc,
  25.     U8 *data,
  26.     U32 count)
  27. {
  28.     U32 crc, byte;
  29.  
  30.     ASSERT(NULL != data);
  31.  
  32.     if (0xFFFFFFFFL == *crc_table) build_crc_table(crc_table);
  33.  
  34.     ASSERT(0x2D02EF8DL == crc_table[255]);
  35.     crc = input_crc;
  36.  
  37.     for (byte = 0; byte < count; ++byte) {
  38.         crc = ((crc >> 8) & 0xFFFFFFL) ^
  39.           crc_table[ (crc ^ data[byte]) & 0xFF ];
  40.     }
  41.     return crc;
  42. }
  43.  
  44. static void
  45. build_crc_table(
  46.     U32 *table)
  47. {
  48.     int byte, bit;
  49.     U32 accum;
  50.  
  51.     ASSERT(NULL != table);
  52.  
  53.     for (byte = 0; byte < 256; ++byte) {
  54.         accum = byte;
  55.  
  56.         for (bit = 0; bit < 8; ++bit) {
  57.             if (accum & 1) accum = (accum >> 1) ^ 0xEDB88320L;
  58.             else accum >>= 1;
  59.         }
  60.         table[byte] = accum;
  61.     }
  62.     ASSERT(0x2D02EF8DL == table[255]);
  63. }
  64.